package cn.hl.hm.controller;

import cn.hl.hm.constants.DriverStatus;
import cn.hl.hm.dao.DriverMapper;
import cn.hl.hm.dao.OrderMapper;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.Order;
import cn.hl.hm.entity.StatisticsDataBean;
import cn.hl.hm.service.StatisticsDataService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import com.vizhuo.utils.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;


/**DCR
 * 
 */
@Controller
@RequestMapping("/statisticeDataController")
public class StatisticeDataController {
	
	private static Logger logger = Logger.getLogger(StatisticeDataController.class);
	@Autowired
	private StatisticsDataService statisticsDataService;
	@Autowired
	private DriverMapper driverMapper;
	@Autowired
	private OrderMapper orderMapper;
	
	//数据统计
	@RequestMapping(value = "/dataStatistics", method = RequestMethod.POST)
	public
	@ResponseBody
	String dataStatistics(
			StatisticsDataBean statisticsDataBean,
    		@RequestParam(value = "limit", required = false) Integer limit,
    		@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("数据统计---数据统计---statisticeDataController/dataStatistics");
		try{
			List<StatisticsDataBean> tae=statisticsDataService.selectStatisticsDataList(statisticsDataBean,limit, pageSize);
			if(tae!=null && tae.size()>0){
				Result.ok(tae, InfoCode.SUCCESS, response);
			}else{
				Result.ok(null, InfoCode.SUCCESS, response);
			}
			
		    return null;
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
			}
			return null;
		}

	//订单数据统计
	@CrossOrigin
	@RequestMapping(value = "/orderInfoStatistics", method = RequestMethod.GET)
	public String orderInfoStatistics(
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("数据统计---数据统计---statisticeDataController/orderInfoStatistics");
		try{
			Driver driver=new Driver();
			driver.setDriverStatus(DriverStatus.PASS.getStatus());
			int driverCount = driverMapper.selectDriverCount(driver);
			int orderCountStaticData = orderMapper.selectOrderCountStaticData(DateUtil.getCurrentDate(), DateUtil.getDateBeforeToday(1) + " 04:00:00");
			Order order = orderMapper.selectOrderInfoStaticData();
			HashMap<String,Object> map=new HashMap<>();
			map.put("driverCount",driverCount);
			map.put("todayOrderCount",orderCountStaticData);
			map.put("orderTotalCount",order.getId());
			map.put("orderTotalMileage",order.getTotalMileage().toString());
			map.put("orderTotalPrice",order.getTotalPrice().toString());
			Result.ok(map, InfoCode.SUCCESS, response);
			return null;
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
}
